#ifndef GAME_H
#define GAME_H

#include "Singleton.h"

#include <QDebug>
#include <QList>

class EquipmentVisitor;

// 设备
class Equipment {
  public:
    virtual QString name()
    {
        return _name;
    }
    virtual void Add(Equipment*){}
    virtual void Remove(Equipment*){}
//    virtual QList<Equipment*>::iterator* CreateIterator(){ return 0; }

    Equipment(QString name)
    {
        _name = name;
    }
    virtual ~Equipment(){}
    virtual void Accept(EquipmentVisitor&) { qInfo() << "no visitor"; }
private:
    QString _name;
};

// 软盘 Leaf类
class FloppyDisk: public Equipment {
public:
    virtual QString name()
    {
        return _name;
    }
    FloppyDisk(QString name): Equipment(name)
    {
        _name = name;
    }
    virtual ~FloppyDisk(){}
    virtual void Add(Equipment*){}
    virtual void Remove(Equipment*){}
    virtual void Accept(EquipmentVisitor& visitor);
//    virtual QList<Equipment*>::iterator* CreateIterator(){ return 0; }
private:
    QString _name;
};

// 容器类
class CompositeEquipment: public Equipment {
public:
    QString name()
    {
        QString str = _name + ": ";

        QList<Equipment*>::iterator it = _equipment.begin();
        for(; it != _equipment.end(); ++it)
        {
            str += (*it)->name() + ", ";
        }

        return str;
    }
    CompositeEquipment(QString name): Equipment(name)
    {
        _name = name;
    }
    virtual ~CompositeEquipment(){}
    void Add(Equipment* e){
        _equipment.append(e);
    }
    void Remove(Equipment* e){
        QList<Equipment*>::iterator it = _equipment.begin();
        for(; it != _equipment.end(); ++it)
        {
            if((*it)->name() == e->name())
            {
                _equipment.erase(it);
                break;
            }
        }
    }
//    virtual QList<Equipment*>::iterator* CreateIterator(){ return 0;}

    QList<Equipment*> _equipment;
    QString _name;
};

// 主板
class Chassis: public CompositeEquipment {
public:
    virtual ~Chassis(){}
    Chassis(QString name): CompositeEquipment(name)
    {
        _name = name;
    }
    virtual void Accept(EquipmentVisitor& visitor);
private:
    QString _name;
};


class EquipmentVisitor {
public:
    EquipmentVisitor(){}
    virtual ~EquipmentVisitor(){}

    virtual void VisitFloppyDisk(FloppyDisk* f) {
        qInfo() << __FUNCTION__ << f->name();
    }
    virtual void VisitChassis(Chassis* c) {
        qInfo() << __FUNCTION__ << c->name();
    }
};

#endif // GAME_H
